<?php

declare(strict_types=1);

namespace app\controller\api;

use app\model\AdminUser;
use app\util\Sms;
use app\util\Tools;
use think\facade\Db;
use think\Response;
use app\util\WechatService;
use app\model\AdminUserData;

/**
 * 驾校班型管理
 */
class JxAdminClass extends Base
{
    //版型列表
    public function classList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');
        //班型名称
        if (isset($param['class_name']) && $param['class_name'] != '') {
            $where[] = ['class_name', 'like', '%' . $param['class_name'] . '%'];
        }
        //班型 1普通 班型 2活动班型
        if (isset($param['class_type']) && $param['class_type'] != '') {
            $where[] = ['class_type', 'like', '%' . $param['class_type'] . '%'];
        }
        //驾考类型
        if (isset($param['jiaokao_type']) && $param['jiaokao_type'] != '') {
            $where[] = ['jiaokao_type', 'like', '%' . $param['jiaokao_type'] . '%'];
        }

        //驾考类型
        if (isset($param['online_status']) && $param['online_status'] != '') {
            $where[] = ['online_status', '=', $param['online_status']];
        }


        $list = Db::table('jx_class')->page((int)$page, (int)$limit)->field('id,class_name,jiaokao_type,class_image,class_type,class_image,subject_two_fixed_hour,subject_three_fixed_hour,full_payment_amount,online_status')->where($where)->order('id desc')->select();
        $count = Db::table('jx_class')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //添加或者修改班型
    public function addClass()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $class_name = $param['class_name'];  //版型名称
        $class_image = $param['class_image']; //班型图片
        $class_type = $param['class_type']; //班型类型  1普通班型 2活动班型
        $subject_two_fixed_hour = $param['subject_two_fixed_hour']; //科目二固定课时
        $subject_two_give_hour = $param['subject_two_give_hour']; //科目二赠送课时
        $subject_three_fixed_hour = $param['subject_three_fixed_hour']; //科目三固定课时
        $subject_three_give_hour = $param['subject_three_give_hour']; //科目三赠送课时
        $full_payment_amount = $param['full_payment_amount']; //全款金额
        $one_full_ratio = $param['one_full_ratio']; //一期百分比率
        $two_full_ratio = $param['two_full_ratio']; //二期百分比率
        $three_full_ratio = $param['three_full_ratio']; //三期百分比率
        $full_stages_amount = $param['full_stages_amount']; //分期全款金额
        $one_stages_ratio = $param['one_stages_ratio']; //分期一期百分比率
        $two_stages_ratio = $param['two_stages_ratio']; //分期二期百分比率
        $three_stages_ratio = $param['three_stages_ratio']; //分期三期百分比率
        $coach_rebate = $param['coach_rebate']; //教练返佣
        $invite_rebate = $param['invite_rebate']; //邀请返佣
        $agency_rebate = $param['agency_rebate']; //代理返佣
        $pledge_rebate = $param['pledge_rebate']; //质押金
        $class_description = $param['class_description']; //班型简介
        $cost_details = $param['cost_details']; //费用明细 JSON串  [{name：123，price：100}，{name：456，price：200}]
        $class_contract = $param['class_contract']; //报名合同
        $jiaokao_type = $param['jiaokao_type']; //驾考类型

        $online_status = isset($param['online_status']) && ($param['online_status'] != '') ? $param['online_status'] : 1;

        $class_application_end_time = isset($param['class_application_end_time']) && ($param['class_application_end_time'] != '') ? $param['class_application_end_time'] : '';


        if ($class_name == '' || $class_image == '' || $class_type == '') {
            return $this->buildFailed(401, '参数错误');
        }

        $map['class_name'] = $class_name;
        $map['class_image'] = $class_image;
        $map['class_type'] = $class_type;
        $map['subject_two_fixed_hour'] = $subject_two_fixed_hour;
        $map['subject_two_give_hour'] = $subject_two_give_hour;
        $map['subject_three_fixed_hour'] = $subject_three_fixed_hour;
        $map['subject_three_give_hour'] = $subject_three_give_hour;
        $map['full_payment_amount'] = $full_payment_amount;
        $map['one_full_ratio'] = $one_full_ratio;

        $map['two_full_ratio'] = $two_full_ratio;
        $map['three_full_ratio'] = $three_full_ratio;
        $map['full_stages_amount'] = $full_stages_amount;
        $map['one_stages_ratio'] = $one_stages_ratio;
        $map['two_stages_ratio'] = $two_stages_ratio;
        $map['three_stages_ratio'] = $three_stages_ratio;
        $map['coach_rebate'] = $coach_rebate;

        $map['invite_rebate'] = $invite_rebate;
        $map['agency_rebate'] = $agency_rebate;
        $map['pledge_rebate'] = $pledge_rebate;
        $map['class_description'] = $class_description;
        $map['cost_details'] = $cost_details;
        $map['class_contract'] = $class_contract;
        $map['jiaokao_type'] = $jiaokao_type;
        $map['class_application_end_time'] = $class_application_end_time;
        $map['online_status'] = $online_status;
        if (isset($param['id']) && $param['id'] != '') {
            $map['update_time'] = date('Y-m-d H:i:s', time());
            $res = Db::name('jx_class')->where('id', $param['id'])->save($map);
        } else {
            $where['status'] = array('in', '1,2');
            $row = Db::name('jx_class')->where('class_name', $param['class_name'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该名称已经添加');
            };
            $map['add_time'] = date('Y-m-d H:i:s', time());
            $res = Db::name('jx_class')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }



    //班型详情
    public function classDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_class')->where('id', $param['id'])->find();
        if ($data) {
            $data['cost_details']   = json_decode($data['cost_details'], true);
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除班型
    public function classDel()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_class')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }

    //添加或者修改驾校场地
    public function addTimePart()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }

        $map['add_time'] = date('Y-m-d H:i:s', time());
        $map['time_part'] = $param['time_part'];


        if (isset($param['id']) && $param['id'] != '') {

            $res = Db::table('jx_time_part')->where('id', $param['id'])->save($map);
        } else {
            $where['status'] = array('in', '1,2');
            $row = Db::table('jx_time_part')->where('time_part', $param['time_part'])->where($where)->find();
            if (!empty($row)) {
                return $this->buildFailed(401, '该名称已经添加');
            }
            //添加
            $res = Db::table('jx_time_part')->insert($map);
        }
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }
    //时间段管理
    public function timePartList(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        $page = isset($param['page']) && ($param['page'] != '') ? $param['page'] : 1;
        $limit =  isset($param['limit']) && ($param['limit'] != '') ? $param['limit'] : 10;
        $where['status'] = array('in', '1,2');


        $list = Db::table('jx_time_part')->page((int)$page, (int)$limit)->field('id,time_part,add_time')->where($where)->order('id desc')->select();
        $count = Db::table('jx_time_part')->where($where)->count();
        $data['list'] = $list;
        $data['count'] = $count;
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //时间段详情
    public function TimePartDetail(): Response
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }

        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $data = Db::table('jx_time_part')->where('id', $param['id'])->find();
        if ($data) {
            return $this->buildSuccess($data);
        } else {
            return $this->buildFailed(401, '获取数据失败');
        }
    }

    //删除时间段
    public function TimePartDel()
    {
        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, '参数错误');
        }
        if (isset($param['id']) && $param['id'] == '') {
            return $this->buildFailed(400, 'id 参数错误');
        }
        $res = Db::name('jx_time_part')->where('id', $param['id'])->save(['status' => '3']);
        if ($res) {
            return $this->buildSuccess();
        } else {
            return $this->buildFailed(401, '网络错误');
        }
    }

    /**
     * 获取逾期费用
     */
    public function getOverduePrice(): Response
    {
        $res = Db::table('jx_system')->where('mark', 'overdue_price')->find();
        return $this->buildSuccess($res);
    }
    /**
     * 设置逾期费用
     */
    public function setOverduePrice(): Response
    {
        $param = $this->request->param();
        if (isset($param['overdue_price']) && $param['overdue_price'] != '') {
            $map['overdue_price'] = $param['overdue_price'];
        }
        $res =  Db::table('jx_system')->where('mark', 'overdue_price')->save(['content' => $param['overdue_price']]);
        return $this->buildSuccess();
    }

    /**
     * 陪练费用及设置
     */
    public function setAccompanyRebate(): Response
    {

        $param = $this->request->param();
        if (isset($param['admin_id']) && $param['admin_id'] == '') {
            return $this->buildFailed(400, 'admin_id 参数错误');
        }
        //陪练费用
        if (isset($param['accompany_price']) && $param['accompany_price'] != '') {
            $map['accompany_price'] = $param['accompany_price'];
        }
        //普通用户返佣
        if (isset($param['coach_get_price_rebate']) && $param['coach_get_price_rebate'] != '') {
            $map['coach_get_price_rebate'] = $param['coach_get_price_rebate'];
        }

        if (!empty($map)) {
            foreach ($map as $k => $v) {
                $res = Db::table('jx_system')->where('mark', $k)->save(['content' => $v]);
            }
        } else {
            return $this->buildFailed(401, '参数错误');
        }

        return $this->buildSuccess();
    }

    /**
     * 获取逾期费用
     */
    public function getAccompanyPrice(): Response
    {
        $accompany_price = Db::table('jx_system')->where('mark', 'accompany_price')->find();
        $coach_get_price_rebate = Db::table('jx_system')->where('mark', 'coach_get_price_rebate')->find();
        $data['accompany_price'] = $accompany_price;
        $data['coach_get_price_rebate'] = $coach_get_price_rebate;
        return $this->buildSuccess($data);
    }

    //下架班型
    public function setDownClass(): Response
    {
        $param = $this->request->post();
        //1上架 2下架
        $online_status = isset($param['online_status']) && ($param['online_status'] != '') ? $param['online_status'] : 1;
        $id = $param['id']; //用户id
        $res = Db::table('jx_class')->where('id', $id)->save(['online_status' =>$online_status]);
        return $this->buildSuccess();
    }

    //


}
